/*
 * Copyright (c) 2006-2024, RT-Thread Development Team
 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2019-10-24     Magicoe      first version
 * 2020-01-10     Kevin/Karl   Add PS demo
 * 2020-09-21     supperthomas fix the main.c
 *
 */

#include <rtdevice.h>
#include <rtthread.h>
#include "drv_pin.h"
#include "board.h"
#include "dhtxx.h"

#define LEDB_PIN        ((0*32)+10)
#define BUTTON_PIN      ((0*32)+23)
#define LED1_PIN   ((1*32)+14)
#define LED2_PIN   ((1*32)+15)
#define LED3_PIN   ((1*32)+16)

static void sw_pin_cb(void *args);
#define LED_NUM 6

void led1(void)
{
	rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);  /* Set GPIO as Output */
	
	rt_pin_mode(LED2_PIN, PIN_MODE_INPUT_PULLDOWN);
	
	rt_pin_mode(LED3_PIN, PIN_MODE_INPUT_PULLUP);
	
	rt_pin_write(LED1_PIN, PIN_HIGH); 
	
}
void led2(void)
{
	rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);  
	rt_pin_mode(LED1_PIN, PIN_MODE_INPUT_PULLDOWN);  
	rt_pin_mode(LED3_PIN, PIN_MODE_INPUT_PULLUP);
	rt_pin_write(LED2_PIN, PIN_HIGH); 
}
void led3(void)
{
	rt_pin_mode(LED2_PIN, PIN_MODE_OUTPUT);  
	rt_pin_mode(LED3_PIN, PIN_MODE_INPUT_PULLDOWN);  
	rt_pin_mode(LED1_PIN, PIN_MODE_INPUT_PULLUP);
	rt_pin_write(LED2_PIN, PIN_HIGH); 
}
void led4(void)
{
	rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);  
	rt_pin_mode(LED2_PIN, PIN_MODE_INPUT_PULLDOWN);  
	rt_pin_mode(LED1_PIN, PIN_MODE_INPUT);
	rt_pin_write(LED3_PIN, PIN_HIGH); 
}
void led5(void)
{
	rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);  
	rt_pin_mode(LED3_PIN, PIN_MODE_INPUT_PULLDOWN);  
	rt_pin_mode(LED2_PIN, PIN_MODE_INPUT);
	rt_pin_write(LED1_PIN, PIN_HIGH); 
}
void led6(void)
{
	rt_pin_mode(LED3_PIN, PIN_MODE_OUTPUT);  
	rt_pin_mode(LED1_PIN, PIN_MODE_INPUT_PULLDOWN);  
	rt_pin_mode(LED2_PIN, PIN_MODE_INPUT);
	rt_pin_write(LED3_PIN, PIN_HIGH); 
}

void (*led_light[])() ={led1,led2,led3,led4,led5,led6};
static uint8_t cur_idx =0;






rt_device_t dev = RT_NULL;
struct rt_sensor_data sensor_data;
rt_size_t res;

int main(void)
{
#if defined(__CC_ARM)
    rt_kprintf("using armcc, version: %d\n", __ARMCC_VERSION);
#elif defined(__clang__)
    rt_kprintf("using armclang, version: %d\n", __ARMCC_VERSION);
#elif defined(__ICCARM__)
    rt_kprintf("using iccarm, version: %d\n", __VER__);
#elif defined(__GNUC__)
    rt_kprintf("using gcc, version: %d.%d\n", __GNUC__, __GNUC_MINOR__);
#endif

    rt_pin_mode(LEDB_PIN, PIN_MODE_OUTPUT);  /* Set GPIO as Output */

    rt_pin_mode(BUTTON_PIN, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(BUTTON_PIN, PIN_IRQ_MODE_FALLING, sw_pin_cb, RT_NULL);
    rt_pin_irq_enable(BUTTON_PIN, 1);

    rt_kprintf("MCXN947 HelloWorld\r\n");


#ifdef RT_USING_SDIO
    rt_thread_mdelay(2000);
    if (dfs_mount("sd", "/", "elm", 0, NULL) == 0)
    {
        rt_kprintf("sd mounted to /\n");
    }
    else
    {
        rt_kprintf("sd mount to / failed\n");
    }
#endif


#if 0

		dev = rt_device_find("temp_dht11");
    if (dev == RT_NULL)
    {
			 rt_kprintf("found temp_dht11 error!\n");
        return -1;
    }

    if (rt_device_open(dev, RT_DEVICE_FLAG_RDWR) != RT_EOK)
    {
        rt_kprintf("open device failed!\n");
        return  -1;
    }
#else
    dht_device_t sensor = dht_create(PKG_USING_DHTXX_SAMPLE_PIN);



#endif

    while (1)
    {
			#if 1
        rt_pin_write(LEDB_PIN, PIN_HIGH);    /* Set GPIO output 1 */
			  for(int i=0;i<1000;i++)
			{
				rt_hw_us_delay(500);   
			}
                    /* Delay 500mS */
        rt_pin_write(LEDB_PIN, PIN_LOW);     /* Set GPIO output 0 */
			  for(int i=0;i<1000;i++)
			{
				rt_hw_us_delay(500);   
			}
			
			
    if(dht_read(sensor)) {

        rt_int32_t temp = dht_get_temperature(sensor);
        rt_int32_t humi = dht_get_humidity(sensor);

        rt_kprintf("Temp: %d, Humi: %d\n", temp, humi);
    }
    else {
        rt_kprintf("Read dht sensor failed.\n");
    }
#if 0
			 res = rt_device_read(dev, 0, &sensor_data, 1);

        if (res != 1)
        {
            rt_kprintf("read data failed! result is %d\n", res);
            rt_device_close(dev);
            return -1;
        }
        else
        {
             rt_kprintf("temp:%d \n" ,sensor_data.data.temp);
            
        }
#endif
			
			//rt_kprintf("hello world\n");
			#endif
	#if 0
		 
		 led_light[cur_idx]();
		 cur_idx++;
		 if(cur_idx>3)
		 {
			 cur_idx =0;
		 }
		 rt_thread_mdelay(500);  
	#endif
			
			
    }

}

static void sw_pin_cb(void *args)
{
    rt_kprintf("sw pressed\r\n");
}

// end file
